$(function () {
	var gridSetting={
			 url: '../repository/models',
			colModel: [			
				{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
				{ label: 'key', name: 'key', index: 'key', width: 80 }, 			
				{ label: '名称', name: 'name', index: 'name', width: 80 }, 			
				{ label: '创建时间', name: 'createTime', index: 'createTime', width: 80 }, 			
				{ label: '最后修改时间', name: 'lastUpdateTime', index: 'lastUpdateTime', width: 80 }, 			
				{ label: '版本', name: 'version', index: 'VERSION', width: 80 }			
	        ],
	}
	gridSetting=$.extend(true,baseJqGridSetting,gridSetting);
    $("#jqGrid").jqGrid(gridSetting);
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bpmModel: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bpmModel = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		deploye: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var url = "../repository/models/"+id+"/deploye";
			var type="POST";
			$.ajax({
				type: type,
			    url: url,
			    contentType: "application/json",
			    success: function(r){
			    	vm.deployeRule(id,r.deploymentId);
				}
			});
		},
		deployeRule:function(modelId,deploymentId){
			$.ajax({
				type: "POST",
			    url: "../bpm/flow/deploymentModelRule/"+modelId+"/"+deploymentId,
			    contentType: "application/json",
			    success: function(r){
					alert('操作成功', function(index){
						vm.reload();
					});
				}
			});
		},
		download: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var url = "../repository/models/"+id+"/download";
			window.location.href=url;
		},
		updateModel: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
		/*	var url='../modeler.html?modelId='+id;
			window.location.href=url;*/
			parent.layer.open({
				type:2,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "编辑模型",
				area: ['1000px', '880px'],
				shade: 0,
				shadeClose: false,
				content: ['modeler.html?modelId='+id, 'no'],
			});
		},
		saveOrUpdate: function (event) {
			var url;
			var type;
			if(vm.bpmModel.id){//修改
				url = "../repository/models/"+vm.bpmModel.id;
				type ="PUT";
				vm.bpmModel.nameChanged=true;//开启修改名称
			}else{
				url ="../repository/models";
				type="POST";
				var metaInfo={};
				metaInfo.name=vm.bpmModel.name;
				metaInfo.revision=1;
				metaInfo.description=vm.bpmModel.name;
				vm.bpmModel.metaInfo=JSON.stringify(metaInfo);
			}
			/*var url = vm.bpmModel.id == null ? "../repository/models" : "../repository/models/"+vm.bpmModel.id;
			var type = vm.bpmModel.id == null ? "POST" : "PUT";*/
			$.ajax({
				type: type,
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.bpmModel),
			    success: function(r){
			    	if (type=="POST") {//如果是新增，在调用一下自己完善的接口
			    		url = "../repository/models/"+r.id+"/source";
			    		$.ajax({
							type: type,
						    url: url,
						    contentType: "application/json",
						    success: function(r){
						    	
							}
						});
					}
					alert('操作成功', function(index){
						vm.reload();
					});
				}
			});
			
		},
		del: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "DELETE",
				    url: "../repository/models/"+id,
				    contentType: "application/json",
				    success: function(r){
				    	alert('操作成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});
					}
				});
			});
		},
		flowNodeSet() {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
	        var url="../bpm/bpmModelNode.html?modelId="+id;
	        //弹框层
	        layer.open({
	            scrollbar: false,
	            type: 2,
	            title : ["流程节点设置" , true],
	            area: ['80%', '80%'], //宽高
	            content: [url,'no'],
	            shadeClose : false,
	        });
	    },
		getInfo: function(id){
			$.get("../repository/models/"+id, function(r){
                vm.bpmModel = r;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});